Ninja Database Pro supports simple constraints for properties by using attributes. An exception is thrown during save if the constraint is violated. If the constraint violation occurs during an implicit transaction, for example when saving a list, then the entire transaction is rolled back. To ensure a column is unique, add a unique index.
· Caption – When there is a constraint violation, this is thrown in the exception instead of the property name.
· MaxLength – The maximum length for string fields.
· DefaultValue – The property will be filled with this default value on insert if the property is not already filled in.
· Required – This field cannot be inserted or updated if it is null or the default for the type. Example, DateTime cannot be DateTime.MinValue
· NotNull – The field cannot be null even if the type allows it.
The namespace for the constraints are in KellermanSoftware.NinjaDatabasePro.Constraint
C# Example
public class Employee
{
public long EmployeeId { get; set; }
public string Name { get; set; }
[Caption("Phone Number"),MaxLength(10)]
public string PhoneNum { get; set; }
[DefaultValue(1)]
public int FamilyMembers { get; set; }
[Required]
public DateTime BirthDate { get; set; }
[NotNull]
public decimal? CreditScore { get; set; }
public Image Picture { get; set; }
}